﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ExportToVis.CApExportToVisJTCreator
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Runtime.InteropServices;
using Tecnomatix.Engineering;
using Tecnomatix.Planning;


namespace DnProcessSimulateCommands.ExportToVis;

public class CApExportToVisJTCreator
{
  private string jtFileName;

  [DllImport("kernel32.dll")]
  private static extern uint GetSystemDefaultLCID();

  public CApExportToVisJTCreator(string path) => this.jtFileName = path;

  public void CreateJTFile()
  {
    TxJtExportDataEx txJtExportDataEx = new TxJtExportDataEx(this.jtFileName);
    txJtExportDataEx.TargetJtFormat = (TxJtExportDataEx.TxJtFormatEx) 0;
    txJtExportDataEx.TargetJtVersion = TxJtVersionsEx.GetCurrentJtVersion();
    CultureInfo cultureInfo = new CultureInfo((int) CApExportToVisJTCreator.GetSystemDefaultLCID());
    txJtExportDataEx.LanguageAbbreviation = cultureInfo.ThreeLetterWindowsLanguageName;
    TxDocumentEx txDocumentEx = new TxDocumentEx();
    TxObjectList objsToExport = new TxObjectList();
    ((Collection<ITxObject>) objsToExport).Add((ITxObject) TxApplication.ActiveDocument.PhysicalRoot);
    if (objsToExport == null)
      return;
    TxObjectList objectsToExport = CApExportToVisJTCreator.GetObjectsToExport(objsToExport);
    txDocumentEx.SaveCollectionToJtAssembly(objectsToExport, txJtExportDataEx);
  }

  private static TxObjectList GetObjectsToExport(TxObjectList objsToExport)
  {
    TxObjectList objectsToExport = (TxObjectList) null;
    if (objsToExport != null)
    {
      objectsToExport = new TxObjectList(((Collection<ITxObject>) objsToExport).Count);
      foreach (ITxObject itxObject in (Collection<ITxObject>) objsToExport)
      {
        ITxObject export = CApExportToVisJTCreator.ObjectToExport(itxObject);
        if (export != null)
        {
          if (CApExportToVisJTCreator.IsStudy(export) && TxApplication.ViewersManager.GraphicViewer.Cut)
          {
            TxObjectList studySectionCuts = CApExportToVisJTCreator.GetStudySectionCuts();
            if (studySectionCuts != null && ((Collection<ITxObject>) studySectionCuts).Count > 0)
            {
              objectsToExport = studySectionCuts;
              break;
            }
            ((Collection<ITxObject>) objectsToExport).Add(export);
          }
          else
            ((Collection<ITxObject>) objectsToExport).Add(export);
        }
      }
    }
    return objectsToExport;
  }

  internal static bool IsStudy(ITxObject obj)
  {
    ITxObject itxObject = obj;
    if (obj is ITxPlanningObject itxPlanningObject)
      itxObject = itxPlanningObject.EngineeringRepresentation;
    return itxObject == TxApplication.ActiveDocument.PhysicalRoot;
  }

  internal static ITxObject ObjectToExport(ITxObject obj)
  {
    ITxObject export = obj;
    ITxSection sec = obj as ITxSection;
    if (obj == null)
    {
      if (CApExportToVisJTCreator.IsVisible(TxApplication.ActiveDocument.PhysicalRoot.Visibility))
        export = (ITxObject) TxApplication.ActiveDocument.PhysicalRoot;
    }
    else if (sec != null)
      export = CApExportToVisJTCreator.GetSectionCut(sec);
    return export;
  }

  private static ITxObject GetSectionCut(ITxSection sec)
  {
    return TxGraphicSectioningUtilsEx.GetPrimaryComponent((ITxGraphicSectioning) TxApplication.ViewersManager.GraphicViewer, sec) is ITxDisplayableObject primaryComponent ? (!CApExportToVisJTCreator.IsVisible(primaryComponent.Visibility) ? (ITxObject) null : (ITxObject) primaryComponent) : (ITxObject) null;
  }

  private static bool IsVisible(TxDisplayableObjectVisibility secCutVisibility)
  {
    return secCutVisibility == null || secCutVisibility == 1;
  }

  private static TxObjectList GetStudySectionCuts()
  {
    TxObjectList directDescendants = TxApplication.ActiveDocument.PhysicalRoot.GetDirectDescendants((ITxTypeFilter) new TxTypeFilter(typeof (ITxSection)));
    TxObjectList txObjectList = new TxObjectList(1);
    if (directDescendants != null)
    {
      foreach (ITxSection itxSection in (Collection<ITxObject>) directDescendants)
      {
        if (itxSection.Active)
          ((Collection<ITxObject>) txObjectList).Add((ITxObject) itxSection);
      }
    }
    TxObjectList studySectionCuts = new TxObjectList(((Collection<ITxObject>) txObjectList).Count);
    foreach (ITxSection sec in (Collection<ITxObject>) txObjectList)
    {
      ITxObject sectionCut = CApExportToVisJTCreator.GetSectionCut(sec);
      if (sectionCut != null)
        ((Collection<ITxObject>) studySectionCuts).Add(sectionCut);
    }
    return studySectionCuts;
  }
}
